package com.shen.accountbook.bean.temp.biz;

import com.shen.accountbook.bean.temp.SecondSortBean;
import com.shen.accountbook.greendao.DaoSession;
import com.shen.accountbook.greendao.SecondSortBeanDao;
import com.shen.accountbook.greendao.manager.DaoManager;

import java.util.List;

/**
 * author:  shen
 * date:    2018/9/7
 */
public class SecondSortBeanBiz {

    DaoManager mDaoManager;
    DaoSession mDaoSession;
    SecondSortBeanDao mDao;

    public SecondSortBeanBiz(){
        mDaoManager = DaoManager.getInstance();
        mDaoSession = mDaoManager.getSession();
        mDao = mDaoSession.getSecondSortBeanDao();
    }

    /**
     * 插入分类
     *
     * @param firstId
     * @param secondId
     * @param name
     */
    public long insert(long firstId, long secondId, String name){

        long rowId = -1;
        List<SecondSortBean> list = mDao.queryBuilder().where(
                SecondSortBeanDao.Properties.Second_sort_id.eq(secondId)).list();
        if (null == list || list.isEmpty()) {

            SecondSortBean bean = new SecondSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(firstId);
            bean.setSecond_sort_id(secondId);
            bean.setSecond_sort_name(name);

            rowId = mDao.insert(bean);

        } else {
            rowId = list.get(0).getId();
        }
        return rowId;
    }

    /**
     * 1 01
     *
     * 插入分类 -- 根据"first_sort_id"加1后 作为first_sort_id插入
     * @param name
     */
    public boolean insertAuto(long firstId, String name){

        long rowId;
        List<SecondSortBean> list = mDao.queryBuilder()
                .orderDesc(SecondSortBeanDao.Properties.Second_sort_id).list();
        if (null == list || list.isEmpty()) {

            SecondSortBean bean = new SecondSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(firstId);
            bean.setSecond_sort_id(firstId * 100 + 10l);
            bean.setSecond_sort_name(name);
            rowId = mDao.insert(bean);

        } else {
            SecondSortBean beanTemp = list.get(0);

            SecondSortBean bean = new SecondSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(firstId);
            bean.setSecond_sort_id(beanTemp.getSecond_sort_id() + 1);
            bean.setSecond_sort_name(name);
            rowId = mDao.insert(bean);
        }

        if(rowId > 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取全部的
     * @return
     */
    public List<SecondSortBean> getSecondSortBean(){
        List<SecondSortBean> beanList = mDao.queryBuilder().list();
        return beanList;
    }

    /**
     * 20个 为一页
     * @param offset
     * @return
     */
    public List<SecondSortBean> getSecondSortBean(int offset){
        List<SecondSortBean> beanList = mDao.queryBuilder().offset(offset * 20).limit(20).list();
        return beanList;
    }


    /**
     * 根据第一分类获取第二分类
     * @param firstId
     * @return
     */
    public List<SecondSortBean> getBeanByFirstId(long firstId){
        List<SecondSortBean> beanList = mDao.queryBuilder()
                .where(SecondSortBeanDao.Properties.First_sort_id.eq(firstId)).list();
        return beanList;
    }
}
